package com.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.video.dao.CommentsMapper;
import com.video.dao.LikedCommentMapper;
import com.video.dao.UsersMapper;
import com.video.dao.VlogsMapper;
import com.video.dto.CommentDTO;
import com.video.entity.Comments;
import com.video.entity.LikedComment;
import com.video.entity.User;
import com.video.entity.Vlogs;
import com.video.exception.VideoException;
import com.video.result.AppHttpCodeEnum;
import com.video.result.ResultVO;
import com.video.service.CommentsService;
import com.video.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (Comments)表服务实现类
 *
 * @author makejava
 * @since 2022-12-14 21:02:16
 */
@Service("commentsService")
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {

    @Resource
    private VlogsMapper vlogsMapper;

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private LikedCommentMapper likedCommentMapper;

    @Override
    public List<Comments> getCommentsByVlogId(Long id) {
        LambdaQueryWrapper<Comments> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comments::getVlogId, id);
        queryWrapper.orderByDesc(Comments::getId);
        return list(queryWrapper);
    }

    @Override
    public Long countVlogComments(Long vlogId) {
        LambdaQueryWrapper<Comments> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comments::getVlogId, vlogId);
        return count(queryWrapper);
    }

    @Override
    public ResultVO deleteComment(CommentDTO commentDTO) {
        LambdaQueryWrapper<Comments> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comments::getId, commentDTO.getCommentId());
        queryWrapper.eq(Comments::getVlogId, commentDTO.getVlogId());
        queryWrapper.eq(Comments::getUserId, commentDTO.getUserId());

        remove(queryWrapper);
        return ResultVO.success();
    }

    @Override
    public ResultVO addComment(Long userId, Long vlogId, String content) {

        User user = usersMapper.selectById(userId);
        if (Objects.isNull(user)) {
            throw new VideoException(AppHttpCodeEnum.USER_NOT_EXIST);
        }
        Vlogs vlog = vlogsMapper.selectById(vlogId);
        if (Objects.isNull(vlog)) {
            throw new VideoException(AppHttpCodeEnum.VLOG_NOT_EXIST);
        }
        if (!StringUtils.hasText(content)) {
            throw new VideoException(AppHttpCodeEnum.COMMENT_NOT_NULL);
        }

        Comments comments = new Comments()
                .setUserId(userId)
                .setVlogId(vlogId)
                .setContent(content);
        save(comments);
        return ResultVO.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO likeComment(Long userId, Long commentId) {
        User user = usersMapper.selectById(userId);
        if (Objects.isNull(user)) {
            throw new VideoException(AppHttpCodeEnum.USER_NOT_EXIST);
        }
        Comments comment = getById(commentId);
        if (Objects.isNull(comment)) {
            throw new VideoException(AppHttpCodeEnum.COMMENT_NOT_EXIST);
        }

        LambdaQueryWrapper<LikedComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LikedComment::getUserId, userId);
        queryWrapper.eq(LikedComment::getCommentId, commentId);
        LikedComment likedComment = likedCommentMapper.selectOne(queryWrapper);

        if (Objects.isNull(likedComment)) {
            //用户未点过赞
            //更新用户点赞状态
            LikedComment one = new LikedComment()
                    .setCommentId(commentId)
                    .setUserId(userId)
                    .setStatus(1);
            likedCommentMapper.insert(one);

            //更新评论点赞数
            Comments comments = BeanCopyUtils.copyBean(comment, Comments.class);
            comments.setLikeCounts(comments.getLikeCounts() + 1);
            updateById(comments);
        } else {
            //用户点过赞
            Integer status = likedComment.getStatus();
            if (0 == status) {
                // 点赞
                likedComment.setStatus(1);
                comment.setLikeCounts(comment.getLikeCounts() + 1);
            } else {
                // 取消点赞
                likedComment.setStatus(0);
                comment.setLikeCounts(comment.getLikeCounts() - 1);
            }
            //更新点赞状态
            likedCommentMapper.updateById(likedComment);
            //更新点赞数
            updateById(comment);
        }

        return ResultVO.success();
    }

}

